//万昱柔 2025/3/10
const { ccclass, property } = cc._decorator;
import { Common } from "./Common.js";

cc.Class({
    extends: cc.Component,

    properties: {
        images: [cc.Node], // 三张图片的节点数组

        imageSpacing: 300, // 图片之间的间距

        minScale: 0.8, // 非居中图片的缩放比例

        minOpacity: 150, // 非居中图片的透明度
    },


    onLoad() {
        // 监听触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    },

    start() {
        this.initInfo();
        // 初始化图片位置和缩放
        this.updateImagesPosition();
        Common.curBird = this.currentIndex;
    },

    initInfo() {
        this.startTouchPos = null; // 触摸起始位置
        this.currentIndex = 1; // 当前居中的图片索引
        this.isDragging = false; // 是否正在拖动
        this.startTime = 0; // 触摸开始时间
        this.endTime = 0; // 触摸结束时间
        this.swipeVelocity = 0; // 滑动速度
    },

    onTouchStart(event) {
        this.isDragging = true;
        this.startTouchPos = event.getLocation(); // 获取当前的触摸点坐标
        this.startTime = Date.now(); // 获取滑动开始时间
    },

    onTouchMove(event) {
        if (!this.isDragging) return;

        const currentTouchPos = event.getLocation();
        const deltaX = currentTouchPos.x - this.startTouchPos.x;

        // 拖动时实时更新图片位置
        for (let i = 0; i < this.images.length; i++) {
            const image = this.images[i];
            const targetX = (i - this.currentIndex) * this.imageSpacing + deltaX;
            image.x = targetX;
        }
    },

    onTouchEnd(event) {
        if (!this.isDragging) return;

        this.isDragging = false;
        const endTouchPos = event.getLocation();
        const deltaX = endTouchPos.x - this.startTouchPos.x;
        this.endTime = Date.now(); // 记录滑动结束时间

        // 计算滑动速度
        const deltaTime = (this.endTime - this.startTime) / 1000; // 转换为秒
        this.swipeVelocity = deltaX / deltaTime;

        // 判断滑动方向
        if (Math.abs(this.swipeVelocity) > 500) {
            // 快速滑动，根据速度切换图片
            if (this.swipeVelocity > 0) {
                this.moveToPrevious();
            } else {
                this.moveToNext();
            }
        } else {
            // 慢速滑动，根据滑动距离切换图片
            if (deltaX > 50) {
                this.moveToPrevious();
            } else if (deltaX < -50) {
                this.moveToNext();
            } else {
                // 滑动距离不足，回弹到当前居中图片
                this.updateImagesPosition();
            }
        }

        Common.curBird = this.currentIndex;
    },

    moveToPrevious() {
        if (this.currentIndex > 0) {
            this.currentIndex--;
            this.updateImagesPosition();
        } else {
            // 已经到最左边，回弹
            this.updateImagesPosition();
        }
    },

    moveToNext() {
        if (this.currentIndex < this.images.length - 1) {
            this.currentIndex++;
            this.updateImagesPosition();
        } else {
            // 已经到最右边，回弹
            this.updateImagesPosition();
        }
    },

    updateImagesPosition() {
        // 根据当前居中的图片索引调整图片位置、缩放和透明度
        for (let i = 0; i < this.images.length; i++) {
            const image = this.images[i];
            const targetX = (i - this.currentIndex) * this.imageSpacing;
            const targetScale = i === this.currentIndex ? 1 : this.minScale;
            const targetOpacity = i === this.currentIndex ? 255 : this.minOpacity;

            cc.tween(image)
                .to(0.3, { position: cc.v3(targetX, image.position.y, 0), scale: targetScale, opacity: targetOpacity })
                .start();
        }
    }

});
